package com.server.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.server.db.imp.YjfjImp;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_ByteBuff;
import com.sandking.tools.SK_Plus;
import java.util.List;
import com.server.db.imp.FjlxImp;
import java.util.HashMap;

/**
 * 附件类型
 */
public class Fjlx {

	public static final String TABLE_NAME = "附件类型";
	public static final String CLASS_NAME = "Fjlx"; 
	static final int MAX_GET = Short.MAX_VALUE;
	
	//索引名称
	public static final String  Id_IndexName = "id";
	
	//对象版本号	
	private int _v = 0;
	public int _nextVer(){
		if(_v > MAX_GET) _v = 0;
		return ++_v;
	}
	
	public int _getVer(){
		return _v;
	}
	
	//是否是源对象
	private boolean _source = false;
	
	public boolean is_source() {
		return _source;
	}

	public void set_source(boolean _source) {
		this._source = _source;
	}
	
	//Cache 中的索引
	private String  id_Unique = null;
	public Fjlx updatePrimaryIndex(){
		return updatePrimaryIndex(this);
	}
	
	public static List<Fjlx> updatePrimaryIndex(List<Fjlx> fjlxs){
		for(Fjlx fjlx : fjlxs){
			updatePrimaryIndex(fjlx);	
		}
		return fjlxs;
	}
	
	public static Fjlx updatePrimaryIndex(Fjlx fjlx){
		String field = SK_Plus.b(fjlx.getId()).e();
		fjlx.setId_Unique(field);
		
		return fjlx;
	}
	
	
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/* 可能需要补救的字段 */
	private SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	
	public SK_ByteBuff getSK_ByteBuff(){
		return this.byteBuff;
	}
	
	/** id */
	private int id;
	
	/** 名称 */
	private String mc;
	
	
	public Fjlx() {
		super();
		this._source = true;
	}
	
	public Fjlx(int id, String mc) {
		super();
		this.id = id;
		this.mc = mc;
		this._source = true;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public void setUpdateColumns(Map<String, Object> updateColumns) {
		this.updateColumns = updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		changeSetId(id);
		if(!this._source){
			byteBuff.writeInt(1);
			byteBuff.writeInt(id);
		}
	}
	
	private void changeSetId(int id) {
		this.id = id;
		if(!this._source){
			addUpdateColumn("id",id);
		}
	}
	
	public void changeIdWith(int id){
		this.id += id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(2);
			byteBuff.writeInt(id);
		}
	}
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(3);
			byteBuff.writeInt(id);
			byteBuff.writeInt(min);
		}	
	}
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(4);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
		}
	}
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		changeSetId(this.id);
		//不是源对象才记录变更操作
		if(!this._source){
			byteBuff.writeInt(5);
			byteBuff.writeInt(id);
			byteBuff.writeInt(max);
			byteBuff.writeInt(min);
		}	
	}	
	public String getMc() {
		return mc;
	}
	
	public void setMc(String mc) {
		changeSetMc(mc);
		if(!this._source){
			byteBuff.writeInt(6);
			byteBuff.writeString(mc);
		}
	}
	
	private void changeSetMc(String mc) {
		this.mc = mc;
		if(!this._source){
			addUpdateColumn("名称",mc);
		}
	}
	
	
	//id
	public List<Yjfj> getYjfjsFkFjlx_id(){
		return YjfjImp.getByFjlx_id(id);
	}
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	/**
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	*/
	public Fjlx clone(){
		Fjlx fjlx = new Fjlx();
		fjlx.setId(this.getId());
		fjlx.setMc(this.getMc());
		fjlx._v = _v;
		fjlx._source = false;
		return fjlx.updatePrimaryIndex(); 
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("mc", this.mc);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("名称", this.mc);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
	    byteBuff.writeInt(this.id);
	    byteBuff.writeString(this.mc);
	    return byteBuff.toByte();
    }
    
    public byte[] toSKBytes(){
    	SK_ByteBuff byteBuff = SK_ByteBuff.allocate();
    	byteBuff.writeMap(toMap());
	    return byteBuff.toByte();
    }
    
     public static Fjlx createForBytes(byte[] _byte) throws Exception {
     	SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     	Fjlx fjlx = new Fjlx();
	    fjlx.id = byteBuff.readInt(null);
	    fjlx.mc = byteBuff.readString(null);
	    return fjlx.updatePrimaryIndex();
     }
     
     public static Fjlx createForSKBytes(byte[] _byte){
     		SK_ByteBuff byteBuff = SK_ByteBuff.wrap(_byte);
     		@SuppressWarnings("unchecked")
     		Map<String,Object> map = byteBuff.readMap(null);
	     	Fjlx fjlx = Fjlx.createForMap(map);
		    return fjlx.updatePrimaryIndex();
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Fjlx> createForColumnNameList(List<Map<String, Object>> list){
    	List<Fjlx> fjlxs = new ArrayList<Fjlx>();
		for (Map<String, Object> map : list) {
			fjlxs.add(createForColumnNameMap(map));
		}
		return fjlxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Fjlx createForColumnNameMap(Map<String, Object> map){
    	Fjlx obj = new Fjlx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.mc = SK_Map.getString("名称", map);
        return obj.updatePrimaryIndex();
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Fjlx> createForList(List<Map<String, Object>> list){
    	List<Fjlx> fjlxs = new ArrayList<Fjlx>();
		for (Map<String, Object> map : list) {
			fjlxs.add(createForColumnNameMap(map));
		}
		return fjlxs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Fjlx createForMap(Map<String, Object> map){
    	Fjlx obj = new Fjlx();
	    obj.id = SK_Map.getInt("id", map);
	    obj.mc = SK_Map.getString("mc", map);
        return obj.updatePrimaryIndex();
    }
    
    public static Fjlx createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Fjlx> createForJson(List<String> jsons){
    	List<Fjlx> fjlxs = new ArrayList<Fjlx>();
    	for(String json : jsons){
    		fjlxs.add(createForJson(json));
    	}
    	return fjlxs;
    }
    
    /** 级联删除(延迟入库) */
    public boolean deleteAndSon(){
    	return false;
    }
    
    /** 级联删除(及时入库) */
    public boolean deleteAndSonAndFlush(){
    	return false;
    }
    
    /** 延迟插入数据库 */
    public Fjlx insert(){
    	return FjlxImp.insert(this);
    }
    /** 延迟更新数据库 */
    public Fjlx update(){
    	return FjlxImp.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return FjlxImp.delete(this);
    }
    /** 即时插入数据库 */
    public Fjlx insertAndFlush(){
    	return FjlxImp.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Fjlx updateAndFlush(){
    	return FjlxImp.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return FjlxImp.deleteAndFlush(this);
    }
    
    
    
    //--------------------------------Cache 中的索引 GET & SET
	public String getId_Unique(){
		return id_Unique;
	}
	
	public void setId_Unique(String id_Unique){
		this.id_Unique = id_Unique;
	}
	
	
	
	/** 取得改变的索引 */
	public Map<String,String> getUpdateIndexs(){
		Map<String,String> indexNames = new HashMap<String,String>();
		if(!this.getId_Unique().equals(SK_Plus.b(id).e())){
			indexNames.put(Fjlx.Id_IndexName,SK_Plus.b(id).e());
			indexNames.put("isUpdtaeUnique","true");
		}
			
		return indexNames;
	}
}